/*
 * This file (AkActuator.java) is protected by the GNU GPL licence (v2). 
 * Please read the license.txt attached for further informations. 
 * 
 */

package alkwarel.objects;

import java.util.Enumeration;

import artx.objects.AxActuator;
import artx.objects.AxArtspace;
import artx.utils.AxLog;

/**
 * @author Xavier Gouchet
 * 
 */
public abstract class AkActuator extends AxActuator {

	/**
	 * Standard Constructor.
	 * 
	 * @param id
	 *            the object's id
	 * @param name
	 *            the object's name
	 * @param requirements
	 *            the actuator's requirements
	 */
	public AkActuator(int id, String name, String[] requirements) {
		super(id, name, requirements);
	}
	
	/**
	 * Apply the actuator on the artspace.
	 * 
	 * @param a
	 *            the artspace on which the actuator is applied
	 * @return boolean whether the method has been completed successfully or not
	 */
	public abstract boolean Apply(AkArtspace a);

	/**
	 * @see artx.objects.AxActuator#Apply(artx.objects.AxArtspace)
	 */
	public final boolean Apply(AxArtspace a) {
		if (a.getClass() == AkArtspace.class) {
			AkArtspace ak = (AkArtspace) a;
			if (this.CheckRequirements(ak)){
				return this.Apply(ak);
			}else{
				return false;
			}
		} else {
			AxLog.Debug("AkActuator::Apply =>> " +
					"Trying to Apply an AkFilter on a non AkArtspace");
			return false;
		}
	}
	
	/**
	 * @param a
	 * @return boolean
	 */
	protected final boolean CheckRequirements(AkArtspace a){		
		if ((this.REQUIREMENTS==null)||(this.REQUIREMENTS.size()==0)) return true;
		
		AxLog.Debug("AkActuator::CheckRequirements =>> Checking actuator : "+
				this.REQUIREMENTS.size()+" requirements");
		
		Enumeration<String> keys = this.REQUIREMENTS.keys();
		while (keys.hasMoreElements()){
			String name = keys.nextElement();
			String type = this.REQUIREMENTS.get(name);
			if (!a.EnsureParameter(name,type)){
				AxLog.Debug("AkActuator::CheckRequirements =>> missing parameter \""+name+"\"");
				return false;
			}
		}
		AxLog.Debug("AkActuator::CheckRequirements =>> all requirements fulfilled");
		return true;
	}

}
